package com.ja.openchat.ui.found;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.dueeeke.videocontroller.StandardVideoController;
import com.dueeeke.videoplayer.player.VideoView;

public class CircleStandardVideoController extends StandardVideoController {

    public CircleStandardVideoController(@NonNull Context context) {
        super(context);
    }

    public CircleStandardVideoController(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CircleStandardVideoController(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
//        if(blogInfo!=null){
//            PreviewCircleMediaActivity.previewMedia(getContext(),blogInfo,0);
//        }
        if(onClickListener!=null){
            onClickListener.onClick(null);
        }
        return true;
    }

    private View.OnClickListener onClickListener;
    public void setOnClick(View.OnClickListener listener){
        onClickListener = listener;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        return true;
    }

    private final long DELAY = 1000;
    private Runnable retryRunable = new Runnable() {
        @Override
        public void run() {
            long position = mControlWrapper.getCurrentPosition();
            mControlWrapper.start();
            if(position>0){
                mControlWrapper.seekTo(position);
            }
            postDelayed(this,DELAY);
        }
    };

    @Override
    protected void onPlayStateChanged(int playState) {
        super.onPlayStateChanged(playState);
        removeCallbacks(retryRunable);
        switch (playState) {
            //调用release方法会回到此状态
            case VideoView.STATE_IDLE:
                mLockButton.setSelected(false);
                mLoadingProgress.setVisibility(GONE);
                break;
            case VideoView.STATE_PLAYING:
            case VideoView.STATE_PAUSED:
            case VideoView.STATE_PREPARED:
            case VideoView.STATE_BUFFERED:
                mLoadingProgress.setVisibility(GONE);
                break;
            case VideoView.STATE_ERROR:
                mLoadingProgress.setVisibility(VISIBLE);
                break;
            case VideoView.STATE_PREPARING:
            case VideoView.STATE_BUFFERING:
                mLoadingProgress.setVisibility(VISIBLE);
                postDelayed(retryRunable,DELAY);
                break;
            case VideoView.STATE_PLAYBACK_COMPLETED:
                mLoadingProgress.setVisibility(GONE);
                mLockButton.setVisibility(GONE);
                mLockButton.setSelected(false);
                break;
        }
    }
}
